package com.sumainfor.pc.cfo.service.impl;

import com.google.common.collect.Lists;
import com.sumainfor.common.entity.SearchEntity;
import com.sumainfor.common.util.*;
import com.sumainfor.common.utlis.HtmlUtil;
import com.sumainfor.pc.cfo.feign.BaseMarketFeign;
import com.sumainfor.pc.cfo.feign.extend.GmioFeign;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author meng
 * @Create 2019/6/25 19:07
 * 财务部的搜索
 */
@Service
public class SearchService {

    @Autowired
    private GmioFeign gmioFeign;

    @Autowired
    private BaseMarketFeign marketFeign;

    public JsonResult searchForCw(String keyWord) {

        List<SearchEntity> dataList = Lists.newArrayList();
        //搜索字段不为空
        if (!StringUtils.isBlank(keyWord)) {
            Map<String, Object> params = new HashMap<>();
            //通过姓名和手机号去查询租客id
            List<String> tenIdList = marketFeign.getTenantInfoByNameOrPhone(keyWord);
            //如果能查到, 传入参数
            if (tenIdList != null && tenIdList.size() > 0) {
                params.put("tenIdList", tenIdList);
            } else {
                List<String> tenantInfoByPaId = marketFeign.getTenantInfoByPaId(keyWord);
                if (tenantInfoByPaId != null && tenantInfoByPaId.size() > 0) {
                    params.put("tenIdList", tenantInfoByPaId);
                } else {
                    params.put("HouId", keyWord);
                }
            }

            //查询房源收入
            SearchEntity Search1 = new SearchEntity();
            List<Map<String, Object>> inByKeyWord = gmioFeign.getOutAndInByHouseId(params, 1);
            Search1.setName(String.format("房源收入(%d)", inByKeyWord.size()));
            Search1.setRouter("/sys-director/billaudit");
            Search1.setList(inByKeyWord.stream().map(myItem -> {
                StringBuffer myItemStr = new StringBuffer();
                DateUtils.formatDateByMap(myItem);
                myItemStr.append(HtmlUtil.BR)
                        .append("账单编号:").append(MapUtils.getString(myItem, "BiId"))
                        .append(HtmlUtil.BR)
                        .append("账单金额:").append(MapUtils.getString(myItem, "BiMoney"))
                        .append(HtmlUtil.BR)
                        .append("账单详情:").append(MapUtils.getString(myItem, "BillExplain"));

                return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "BiId"), Color.red);

            }).collect(Collectors.toList()));
            dataList.add(Search1);


            //通过姓名和手机号去查询业主id
            params.clear();
            List<String> ownerIdList = marketFeign.getOwnerIdByNameOrPhone(keyWord);
            //如果能查到, 传入参数
            if (ownerIdList != null && ownerIdList.size() > 0) {
                params.put("ownerIdList", ownerIdList);
            } else {
                List<String> ownerIdByPaId = marketFeign.getOwnerIdByPaId(keyWord);
                if (ownerIdByPaId != null && ownerIdByPaId.size() > 0) {
                    params.put("ownerIdList", ownerIdByPaId);
                } else {
                    params.put("HouId", keyWord);
                }
            }

            //根据房源编号查询房源支出
            SearchEntity Search2 = new SearchEntity();
            List<Map<String, Object>> outByKeyWord = gmioFeign.getOutAndInByHouseId(params, 2);
            Search2.setName(String.format("房源支出(%d)", outByKeyWord.size()));
            Search2.setRouter("/sys-director/billaudit");
            Search2.setList(outByKeyWord.stream().map(myItem -> {
                StringBuffer myItemStr = new StringBuffer();
                DateUtils.formatDateByMap(myItem);
                myItemStr.append(HtmlUtil.BR)
                        .append("账单编号:").append(MapUtils.getString(myItem, "BiOutId"))
                        .append(HtmlUtil.BR)
                        .append("账单金额:").append(MapUtils.getString(myItem, "BiMoney"))
                        .append(HtmlUtil.BR)
                        .append("账单详情:").append(MapUtils.getString(myItem, "BillExplain"));

                return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "BiOutId"), Color.red);

            }).collect(Collectors.toList()));
            dataList.add(Search2);
        }

        return JsonResult.success(dataList);
    }

}
